<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/7/2
 * Time: 15:35
 */

namespace App\HttpController\Admin;


use App\Services\AppService;
use App\Services\ReponseCodeService;
use EasySwoole\Validate\Validate;

class App extends AdminBaseController
{
    /**
     * showdoc
     * @catalog 后台/应用管理
     * @title  获取应用
     * @description 获取应用
     * @method GET
     * @url /admin/app
     * @param appname  是 string 应用名字
     * @param password 是 string 密码
     * @return_param  code int 状态码(200:成功0:失败400:参数错误)
     * @return_param  username 用户名
     * @return_param  realname 真实姓名
     * @return_param  head 头像
     * @return_param  mobile 手机号
     * @return_param  created_at 创建时间
     * @return_param  updated_at更新时间
     * @remark
     * @number 1
     */
    public function list(){
        $admin = $this->adminInfo;
        $param =  $this->param;
        $valitor = new Validate();
        $valitor->addColumn('page', '页码')->required('页码错误');
        $valitor->addColumn('limit','每页条数')->required('每页条数');

        $bool = $valitor->validate($param);
        if(!$bool){
            return $this->writeJson(ReponseCodeService::CODE_BAD_REQUEST, null, $valitor->getError()->__toString());
        }

        $appService = new AppService();
        $reponse = $appService->list($param['page'],$param['limit'],$admin['id']);
        if(!is_array( $reponse)){
            return $this->writeJson(ReponseCodeService::CODE_BAD_REQUEST, null, 'faile');
        }
        return   $this->writeJson(ReponseCodeService::CODE_OK, $reponse, "sucess");
    }

    /**
     * showdoc
     * @catalog 后台/应用管理
     * @title  添加应用
     * @description 添加应用
     * @method POST
     * @url /admin/app
     * @param appname  是 string 应用名字
     * @return_param  code int 状态码(200:成功0:失败400:参数错误)
     * @return_param  id 应用的id
     * @return_param  appname 应用名字
     * @return_param  access_key access_key，可作为应用的唯一标识
     * @return_param  secret_key 秘钥
     * @return_param  status 应用状态，状态：0未启用，1启用，2禁用
     * @return_param  created_at 创建时间
     * @return_param  updated_at更新时间
     * @remark
     */
    public function createApp(){
        $admin = $this->adminInfo;
        $param =  $this->param;
        $valitor = new Validate();
        $valitor->addColumn('appname', 'appname')->required('appname is require');
        $bool = $valitor->validate($param);
        if(!$bool){
            return $this->writeJson(ReponseCodeService::CODE_BAD_REQUEST, null, $valitor->getError()->__toString());
        }

        $appService = new AppService();
        $ret = $appService->createApp($param,$admin);
        if(!is_array( $ret)){
            return $this->writeJson($ret, null, '');
        }
        return $this->writeJson(ReponseCodeService::CODE_OK, $ret, '');
    }

    /**
     * showdoc
     * @catalog 后台/应用管理
     * @title  编辑应用
     * @description  编辑应用
     * @method PUT
     * @url /admin/app
     * @param id 是 int 应用id
     * @param appname 否 string 应用名字
     * @param status 否 int  应用名字
     * @return_param  code int 状态码(200:成功0:失败400:参数错误)
     * @return_param  id 应用的id
     * @return_param  appname 应用名字
     * @return_param  access_key access_key，可作为应用的唯一标识
     * @return_param  secret_key 秘钥
     * @return_param  status 应用状态，状态：0未启用，1启用，2禁用
     * @return_param  created_at 创建时间
     * @return_param  updated_at更新时间
     */
    public function updateApp(){
        $admin = $this->adminInfo;
        $param =  $this->param;
        $valitor = new Validate();
        $valitor->addColumn('id', 'id')->required('id is require');
        $bool = $valitor->validate($param);
        if(!$bool){
            return $this->writeJson(ReponseCodeService::CODE_BAD_REQUEST, null, $valitor->getError()->__toString());
        }

        $appService = new AppService();
        $ret = $appService->updateApp($param,$admin);
        return $this->writeJson($ret, $ret, '');
    }

    /**
     * showdoc
     * @catalog 后台/应用管理
     * @title  删除应用
     * @description  删除应用
     * @method DELETE
     * @url /admin/app
     * @param id 是 int 应用id
     * @return_param  code int 状态码(200:成功0:失败400:参数错误)
     */
    public function deleteApp(){
        $admin = $this->adminInfo;
        $param =  $this->param;
        $valitor = new Validate();
        $valitor->addColumn('appname', 'appname')->required('appname is require');
        $bool = $valitor->validate($param);
        if(!$bool){
            return $this->writeJson(ReponseCodeService::CODE_BAD_REQUEST, null, $valitor->getError()->__toString());
        }

        $appService = new AppService();
        $ret = $appService->delApp($param,$admin);
        return $this->writeJson( $ret, $ret, '');
    }
}